﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data;
namespace LHWebApp.Json
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class SendCommand : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            
            string cardNo = context.Request.Params["cardNo"];
            string msg=context.Request.Params["msg"];
            if (string.IsNullOrEmpty(cardNo) || string.IsNullOrEmpty(msg))
            {
                context.Response.Write("{success: false}");
            }
            else
            {
                try{
                    string[] ss=msg.Split(';');
                    if (ss.Length > 0)
                    {
                        if (ss[0].StartsWith("010"))
                        {
                            Services.SetData(string.Format("UPDATE VEHICLE SET FIXBOUND='{0}' WHERE SIMCARD_NO='{1}'",ss[1],cardNo));
                            //google 坐标偏移
                            string[] cmds = ss[0].Split(',');
                            LngLat lngLat1 = Services.LngLatOffset(double.Parse(cmds[1]), double.Parse(cmds[2]));
                            LngLat lngLat2 = Services.LngLatOffset(double.Parse(cmds[3]), double.Parse(cmds[4]));
                            msg = string.Format("010,{0},{1},{2},{3}", lngLat1.Lng, lngLat1.Lat, lngLat2.Lng, lngLat2.Lat);
                        }
                        else if (ss[0].StartsWith("011"))
                            Services.SetData(string.Format("UPDATE VEHICLE SET FIXBOUND='' WHERE SIMCARD_NO='{0}'", cardNo));
                    }
                    Services.SendMessage(cardNo, msg);
                    context.Response.Write("{success: true}");
                }
                catch (Exception)
                {
                    context.Response.Write("{success: false}");
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
